package me.zhengjie.hr.employeebase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.hr.employeebase.domain.EmployeeBase;
import me.zhengjie.hr.employeebase.repository.EmployeeBaseRepository;
import me.zhengjie.hr.employeebase.service.EmployeeBaseService;
import me.zhengjie.hr.employeebase.service.dto.EmployeeBaseQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Api(tags = "人员基本信息管理")
@RestController
@RequestMapping("/api/employeeBase")
public class EmployeeBaseController {

    private final EmployeeBaseService employeeBaseService;
    @Autowired
    private EmployeeBaseRepository employeeBaseRepository;
    public EmployeeBaseController(EmployeeBaseService employeeBaseService) {
        this.employeeBaseService = employeeBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('employeeBase:list')")
    public void download(HttpServletResponse response, EmployeeBaseQueryCriteria criteria) throws IOException {
        employeeBaseService.download(employeeBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询人员基本信息")
    @ApiOperation("查询人员基本信息")
    @PreAuthorize("@el.check('employeeBase:list')")
    public ResponseEntity<Object> getEmployeeBases(EmployeeBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(employeeBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增人员基本信息")
    @ApiOperation("新增人员基本信息")
    @PreAuthorize("@el.check('employeeBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody EmployeeBase resources){
        if(resources.getId() != null){
            employeeBaseRepository.updateLeave(resources.getId(),new Date(resources.getLeaveTime().getTime()),resources.getLeaveReason(),resources.getLeaveInf());
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        if(resources.getLeaveTime() == null){
            resources.setIsLeave("0");
        }
        else{
            resources.setIsLeave("1");
        }
        return new ResponseEntity<>(employeeBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改人员基本信息")
    @ApiOperation("修改人员基本信息")
    @PreAuthorize("@el.check('employeeBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody EmployeeBase resources) {
        System.out.println("-----2020-5-2--fwxx---zuihou");
        if ( resources.getEmpName() == null) {
            employeeBaseRepository.updateChange(resources.getId(), resources.getCenterBelong());
        }
        else {
            Date timetemp =  employeeBaseRepository.TimeisNull(resources.getId());
            //离职的改为在职
            if (resources.getLeaveTime() == null && timetemp != null) {
                employeeBaseRepository.updateOnjob(resources.getId());
            }
            else {
                employeeBaseService.update(resources);
            }
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除人员基本信息")
    @ApiOperation("删除人员基本信息")
    @PreAuthorize("@el.check('employeeBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        employeeBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

